<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/11 0011
 * Time: 下午 4:41
 */
namespace app\http\middleware;

use app\common\auth\JwtAuth;
use app\common\err\ApiErrDesc;
use app\common\exception\ApiException;
use app\http\response\ResponseJson;

/**
 * 验证token中间件
 * Class Token
 * @package app\http\middleware
 */
class Token{
    use ResponseJson;
    public function handle($request, \Closure $next)
    {
        $token = $request->param('token');
        if($token){
            $jwtAuth = JwtAuth::getInstance();
            $jwtAuth->setToken($token);
            if($jwtAuth->validate() && $jwtAuth->verify()){
                return $next($request);
            }else{
                throw new ApiException(ApiErrDesc::LOGIN_ERR);
            }
        }else{
            throw new ApiException(ApiErrDesc::ERR_PARAMS);
        }


    }
}